package com.qfoud.edu.framwork.stragy.impl;

import com.qfoud.edu.framwork.pojo.BeanParameterDefinition;
import com.qfoud.edu.framwork.pojo.Model;
import com.qfoud.edu.framwork.stragy.base.ParameterTypeStragy;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xiaobobo
 * @title: JavaTypeStragy
 * @projectName cd-java-2303-project-parent
 * @description: TODO
 * @date 2023/5/10  15:26
 */
public class JavaTypeStragy implements ParameterTypeStragy {
    @Override
    public void run(Model model, HttpServletResponse resp, BeanParameterDefinition beanParameterDefinition, HttpServletRequest req, int index, String parameterType, String parameterName, Object... args) {
        //这里就是处理Java类型的参数设置
        javaTypeParameterSet(req, index, parameterType, parameterName, args);
    }

    /**
     * Java类型的参数设置
     * @param req
     * @param index
     * @param parameterType
     * @param parameterName
     * @param args
     */
    private void javaTypeParameterSet(HttpServletRequest req, int index, String parameterType, String parameterName, Object[] args) {
        //如果是Java中的单个的数据类型的话 那么执行下面的函数
        String parameter = req.getParameter(parameterName);
        //获取当前的数据类型
        switch (parameterType){
            case "java.lang.Integer":
            case "int":
                int val=Integer.parseInt(parameter);
                args[index]=val;
                break;
            case "java.lang.String":
            case "String":
                args[index]=parameter;
                break;

            case "java.lang.Float":
            case "float":
                float floatVal = Float.parseFloat(parameter);
                args[index]=floatVal;
                break;

            case "java.lang.Boolean":
            case "boolean":
                boolean b = Boolean.parseBoolean(parameter);
                args[index]=b;
                break;

            case "java.lang.Double":
            case "double":
                double v = Double.parseDouble(parameter);
                args[index]=v;
                break;
        }
    }

}
